import express from "express";
import history from "connect-history-api-fallback";
import path from "node:path";
import config from "./config/config.js";
import middleware from "./middleware/index.js";
import router from "./router/index.js";

const app = express();

// 挂载中间件
app.use(middleware);

// 挂载路由
app.use(config.baseUrl, router);

// 挂载静态资源
app.use("/web", express.static(path.join(process.cwd(), "public")));

// 挂载apidoc文档目录
app.use("/docs", express.static(path.join(process.cwd(), "apidoc")));

// 解决前端路由刷新404问题
app.use(history());

// 捕获所有路由不存在的请求
app.all("*", (req, res) => {
  res.send("请求的资源不存在", 404);
});

// 捕获未处理的Promise异常
process.on("uncaughtException", (err) => {
  console.log("捕获到异常：" + err.message);
});

// 启动服务
app.listen(config.port, () => {
  console.log("服务器启动成功！端口号：" + config.port);
});
